var ocrGml = require("../../utils/ocrGml");
var utils = require("../../utils/utils");
var common = require("./common");

var inTask = {
  run(config) {
    if (this.over()) {
      return;
    }
    this.sign();
    if (config && config.fromAd) {
    } else {
      this.ad();
    }
    if (config && config.fromAward) {
    } else {
      this.award();
    }
  },
  //点击领取
  award() {
    var contents = ["点击领取"];
    var ocrItem = ocrGml.captureExistOne(contents, true);
    if (ocrItem) {
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2, 3, 1000));
      this.run({ fromAward: true });
    }
  },
  over() {
    var contents = ["开心收下", "立即收下"];
    var ocrItem = ocrGml.captureExistOne(contents, true);
    if (ocrItem) {
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2, 3, 1000));
      return true;
    }
  },
  sign() {
    var contents = ["立即签到"];
    var ocrItem = ocrGml.captureExistOne(contents, true);
    if (ocrItem) {
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2, 3, 1000));
      this.run();
    }
  },
  ad() {
    var contents = ["看广告再", "看视频再", "看广告领", "看视频领"];
    var ocrItem = ocrGml.captureExistOne(contents, true);
    if (ocrItem) {
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2, 3, 1000));
      common.ad.init(1, function () {
        var ocrItem = ocrGml.captureExistOne([
          "任务中心",
          "现金收益",
          "展示在首页",
        ]);
        return ocrItem;
      });
      sleep(utils.getRandom(2, 3, 1000));
      this.run({
        fromAd: true,
      });
    }
  },
};

module.exports = inTask;
